#!/usr/bin/env python
# -*- coding:UTF-8 -*-
# Project: devops
#  Author: an00
#  Create: 2018/7/1 14:36


from rest_framework.pagination import PageNumberPagination


class Pagination(PageNumberPagination):
    """
    ?page_size=0获取全部内容
    其他值返回分页数据在results列表中，并且有总数和下一页的链接，如下
    {
        "count": 15,
        "next": "http://127.0.0.1:8000/users/?page=2&page_size=3",
        "previous": null,
        "results": [ ... ]
    }
    """
    page_size = 10
    max_page_size = 1000

    def get_page_size(self, request):
        try:
            page_size = int(request.query_params.get('page_size', -1))
        except ValueError:
            return self.page_size
        else:
            if page_size < 0:
                return self.page_size
            return page_size
